import React, { Component } from 'react';
import { Badge, Button, message, Popconfirm, Popover, Icon, Tabs } from 'antd';
import { messages } from 'share/common';
import SlideFrame from 'components/slide-frame';
import AdvancedTable from 'components/advanced-table';
import 'styles/finance-setting/audit-basis-config/audit-basis-config.scss';
import auditBasisService from 'containers/finance-setting/audit-basis-config/audit-basis-service';
import NewAuditBasis from 'containers/finance-setting/audit-basis-config/new-audit-basis-config';
import BookDateSetting from './book-date-setting'
import { connect } from 'react-redux';
import { permission } from 'containers/finance-setting/permisson';
import ResizeTable from "components/resize-table";

const { SimpleTable } = AdvancedTable;
const { TabPane } = Tabs;

@connect(mapStateToProps)
@permission
class AuditBasisConfig extends Component {

  auditRef = null;

  static propTypes = {};

  state = {
    columns: [
      {
        title: messages('expense-14.key312')/*层级*/,
        dataIndex: 'levelCode',
        render: (text) => text === 'COMPANY' ? messages('expense-14.key320')/*公司*/ : (text === 'TENANT' ? messages('expense-14.key321')/*租户*/ : null)
      },
      {
        title: messages('expense-14.key307')/*所属组织*/,
        dataIndex: 'levelOrgName',
      },
      {
        title: messages('expense-14.key310')/*启用公司*/,
        dataIndex: 'distLevelOrgName',
        render: value => <Popover placement="topLeft" content={value}>{value}</Popover>
      },
      {
        dataIndex: 'enabled', title: messages('common.column.status')/*状态*/,
        render: (text) => {
          return (
            <span>
            <Badge status={text ? 'success' : 'error'}/>
              {text ? messages('common.status.enable')/*启用*/ : messages('expense-14.key311')/*禁用temp*/}
          </span>);
        }
      },
      {
        title: messages('common.operation')/*操作*/,
        dataIndex: 'operate',
        width: '8%',
        render: (value, record) => {
          return (
            <span>
              <Popconfirm
                title={messages('expense-14.key314')/*确认删除吗？*/}
                onConfirm={() => this.handleDelete(record)}>
                <a
                  disabled={this.props.currentMenu && this.props.currentMenu.operationType !== 'RW' || record.readOnly}
                  onClick={(e) => {e.stopPropagation();}}
                >
                  {messages('common.delete')/*删除*/}
                </a>
              </Popconfirm>
            </span>
          );
        }
      }
    ],
    searchForm: [
      {
        id: 'distLevelOrgIds',
        label: messages('expense-14.key310')/*启用公司*/,
        component: 'Chooser',
        props: {
          labelKey: 'name',
          valueKey: 'id',
          type: 'priority_company'
        },
        dataIndex: 'distLevelOrgName',
        parser: value => ({
          distLevelOrgIds: value ? value.map(item => item.id) : null
        })
      },
    ],
    setObj: {
      basis: {
        component: NewAuditBasis,
        title: messages('common.create')/*新建*/
      },
      rule: {
        component: NewAuditBasis,
        title: messages('expense-14.key322')/*默认规则*/
      }
    }
  };


  service = (page, size,searchParams) => {
    return auditBasisService.getAuditBasisConfig(page, size,{ ...searchParams });
  };

  handleDelete = (record) => {
    auditBasisService.deleteAuditBasisConfig(record.baseConfigId)
      .then(() => {
        this.auditRef.getList();
        message.success(messages('common.delete.successful')/*删除成功*/);
      });
  };

  afterCloseSlideFrame = (flag) => {
    flag && this.auditRef.getList();
  };

  handleNewConfig = (flag, record) => {
    const { setObj } = this.state;
    SlideFrame.open({
      title: !record ? setObj[flag].title : messages('common.edit')/*编辑*/,
      content: setObj[flag].component,
      afterClose: this.afterCloseSlideFrame,
      params: {
        mode: flag,
        ...record
      }
    }, this);
  };

  render() {
    const { columns, searchForm } = this.state;
    return (
      <Tabs defaultActiveKey="basicSetting" className="audit-settings full-height-page">
        <TabPane key="basicSetting" tab={messages('expense-14.key323')/*基础配置*/}>
          <section className="audit-basis">
            <SimpleTable
              showUtils
              tableName="audit-basic-config-table"
              show={['tag-list', 'table-utils']}
              onRef={ref => this.auditRef = ref}
              columns={columns}
              service={this.service}
              searchForm={searchForm}
              rowKey="baseConfigId"
              onRow={record => ({
                onClick: () => this.handleNewConfig('basis', record)
              })
              }
            >
              <section className="audit-basis-header">
                <Button
                  disabled={this.isReadOnly()}
                  type="primary"
                  onClick={() => this.handleNewConfig('basis')}
                >
                  {messages('common.create')/*新建*/}
                </Button>
                <span>
                  <Button type="link" ghost onClick={() => this.handleNewConfig('rule')}>
                    <Icon type="file-text" style={{ marginRight: 5 }}/> {messages('expense-14.key322')/*默认规则*/}
                  </Button>
                </span>
              </section>
            </SimpleTable>
          </section>
        </TabPane>
        <TabPane key="bookDateSetting" tab={messages('expense-14.key324')/*记账日期落值配置*/}>
          <BookDateSetting />
        </TabPane>
      </Tabs>
    );
  }
}

function mapStateToProps(state) {
  return {
    currentMenuPermission: state.main.currentMenuPermission,
  };
}

export default AuditBasisConfig;
